package com.qigao.mall.service.impl.admin;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qigao.mall.api.SysRoleResourceService;
import com.qigao.mall.commons.dto.SysRoleResourceCondDto;
import com.qigao.mall.commons.dto.SysRoleResourceDto;
import com.qigao.mall.commons.dto.SysRoleResourceResultDto;
import com.qigao.mall.commons.dto.SysRoleResourceUpdateDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.jdbc.mapper.SysResourceMapper;
import com.qigao.mall.jdbc.mapper.SysRoleResourceMapper;
import com.qigao.mall.jdbc.model.SysResource;
import com.qigao.mall.jdbc.model.SysRoleResource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:luiz
 * @Date: 2018/3/9 10:22
 * @Descripton:
 * @Modify :
 **/
@Service
public class SysRoleResourceServiceImpl implements SysRoleResourceService {
    @Resource
    private SysRoleResourceMapper sysRoleResourceMapper;
    @Resource
    private SysResourceMapper sysResourceMapper;

    @Override
    public Result<String> save(SysRoleResourceUpdateDto sysRoleResourceUpdateDto) {
        sysRoleResourceMapper.deleteByRoleCode(sysRoleResourceUpdateDto.getRoleCode());
        List<SysRoleResource> sysRoleResourceList = new ArrayList<>();
        for (SysRoleResourceDto sysRoleResourceDto : sysRoleResourceUpdateDto.getSysRoleResourceDtoList()) {
            SysRoleResource sysRoleResource = new SysRoleResource();
            BeanUtils.copyProperties(sysRoleResourceDto, sysRoleResource);
            sysRoleResource.setCreateNo(sysRoleResourceDto.getUserNo());
            sysRoleResource.setCreateTime(new Date());
            sysRoleResourceList.add(sysRoleResource);
        }
        sysRoleResourceMapper.insertBatch(sysRoleResourceList);
        return Result.newSuccess();
    }

    @Override
    public Result<String> create(SysRoleResourceDto sysRoleResourceDto) {
        String roleCode = sysRoleResourceDto.getRoleCode();
        String resourceNo = sysRoleResourceDto.getReSourceNo();
        SysRoleResource sysRoleResource = sysRoleResourceMapper.selectByIndex(roleCode, resourceNo);
        if (sysRoleResource != null) {
            return Result.newError(ResultCode.COMMON_DATA_EXISTS);
        }
        SysRoleResource sysRoleResource1 = new SysRoleResource();
        BeanUtils.copyProperties(sysRoleResourceDto, sysRoleResource1);
        sysRoleResource1.setCreateNo(sysRoleResourceDto.getUserNo());
        sysRoleResource1.setCreateTime(new Date());
        sysRoleResourceMapper.insertSelective(sysRoleResource1);
        return Result.newSuccess();
    }

    @Override
    public Result delete(String id) {
        return Result.newSuccess(sysRoleResourceMapper.deleteByPrimaryKey(Integer.valueOf(id)));
    }

    @Override
    public Result deleteByIds(List<String> ids) {
        List<SysRoleResource> sysRoleResourceArrayList = new ArrayList<>();
        for (String id : ids) {
            SysRoleResource sysRoleResource = new SysRoleResource();
            sysRoleResource.setId(Integer.valueOf(id));
            sysRoleResourceArrayList.add(sysRoleResource);
        }
        sysRoleResourceMapper.deleteInBatch(sysRoleResourceArrayList);
        return Result.newSuccess();
    }

    @Override
    public Result<List<SysRoleResourceResultDto>> findByRoleCode(String roleCode) {
        List<SysResource> sysResources = sysResourceMapper.selectAll();
        List<SysRoleResource> sysRoleResources = sysRoleResourceMapper.selectByRoleCode(roleCode);
        return Result.newSuccess(convertDto(sysResources, sysRoleResources));
    }

    private List<SysRoleResourceResultDto> convertDto(List<SysResource> sysResources, List<SysRoleResource> sysRoleResources) {
        List<SysRoleResourceResultDto> sysRoleResourceResultDtos = new ArrayList<>();
        for (SysResource sysResource : sysResources) {
            sysRoleResourceResultDtos.add(buildSingDto(sysResource, sysRoleResources));
        }
        return sysRoleResourceResultDtos;
    }

    private SysRoleResourceResultDto buildSingDto(SysResource sysResource, List<SysRoleResource> sysRoleResources) {
        SysRoleResourceResultDto sysRoleResourceResultDto = buildSingDto(sysResource);
        BeanUtils.copyProperties(sysResource, sysRoleResourceResultDto);
        Integer sourceLevel = sysResource.getResourceLevel() == null ? 0 : sysResource.getResourceLevel();
        sysRoleResourceResultDto.setSourceLevel(String.valueOf(sourceLevel));
        boolean flag = findHasRole(sysRoleResources, sysResource);
        sysRoleResourceResultDto.setChecked(flag);
        return sysRoleResourceResultDto;
    }

    private SysRoleResourceResultDto buildSingDto(SysResource sysResource) {
        SysRoleResourceResultDto sysRoleResourceResultDto = new SysRoleResourceResultDto();
        BeanUtils.copyProperties(sysResource, sysRoleResourceResultDto);
        sysRoleResourceResultDto.setId(sysResource.getResourceNo());
        sysRoleResourceResultDto.setPId(sysResource.getPresourceNo());
        sysRoleResourceResultDto.setName(sysResource.getResourceName());
        return sysRoleResourceResultDto;
    }

    private boolean findHasRole(List<SysRoleResource> sysRoleResources, SysResource sysResource) {
        for (SysRoleResource sysRoleResource : sysRoleResources) {
            if (sysRoleResource.getSourceNo().equals(sysResource.getResourceNo())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Page<SysRoleResourceResultDto> listPage(SysRoleResourceCondDto sysRoleResourceCondDto) {
        //使用PageHelper类进行分页
        PageHelper.startPage(sysRoleResourceCondDto.getPageNum(), sysRoleResourceCondDto.getPageSize());
        Page<SysRoleResource> sysRoleResourcePage = (Page<SysRoleResource>) sysRoleResourceMapper.selectList(sysRoleResourceCondDto);
        Page<SysRoleResourceResultDto> sysRoleResourceResultDtoPage = new Page<>();
        BeanUtils.copyProperties(sysRoleResourcePage, sysRoleResourceResultDtoPage);
        List<SysResource> sysResources = sysResourceMapper.selectAll();
        for (SysResource sysResource : sysResources) {
            sysRoleResourceResultDtoPage.add(buildSingDto(sysResource, sysRoleResourcePage.getResult()));
        }
        return sysRoleResourceResultDtoPage;
    }

    @Override
    public Result<List<SysRoleResourceResultDto>> findByUserNo(String userNo) {
        List<SysResource> sysResources = sysResourceMapper.selectAll();
        List<SysRoleResource> sysRoleResources = sysRoleResourceMapper.selectByUserNo(userNo);
        return Result.newSuccess(convertDto(sysResources, sysRoleResources));
    }
}
